package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPO;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPagedInfoPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddProcessConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteProcessConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditProcessConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetProcessConfigListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.IProcessConfigRepository;
import com.hyzx.qbasic.admin.domain.service.IProcessConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by 黄江华 on 2018/11/28
 */

@Service
public class ProcessConfigServiceImpl implements IProcessConfigService {
    @Autowired
    private IProcessConfigRepository configRepository;

    @Override
    public PageInfo<ProcessConfigPagedInfoPO> getPagedProcessConfigList(GetProcessConfigListReq req) {
        PageInfo<ProcessConfigPagedInfoPO> configList = configRepository.getPagedProcessConfigList(
                req.getPageNum(), req.getPageSize(), req.getProcessCategoryId(), req.getProcessDefinitionName());

        return configList;
    }

    @Override
    public BaseOperationRes addProcessConfig(AddProcessConfigReq req) {
        //检查流程配置是否已存在
        ProcessConfigPO existedConfig = configRepository.getProcessConfigByKey(req.getProcessConfigKey());
        if (existedConfig != null)
            return OperationResUtils.fail("流程配置已存在");

        ProcessConfigPO configPO = new ProcessConfigPO();
        configPO.setProcessConfigKey(req.getProcessConfigKey());
        configPO.setProcessCategoryId(req.getProcessCategoryId());
        configPO.setProcessDefinitionId(req.getProcessDefinitionId());
        configPO.setDescription(req.getDescription());
        configPO.setCreateTime(new Date());
        configPO.setUpdateTime(new Date());

        boolean success = configRepository.addProcessConfig(configPO);
        if (!success)
            return OperationResUtils.fail("新增流程配置失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes deleteProcessConfig(DeleteProcessConfigReq req) {
        boolean success = configRepository.deleteProcessConfig(req.getProcessConfigIdList());
        if (!success)
            return OperationResUtils.fail("删除流程配置失败");

        return OperationResUtils.success();
    }

    @Override
    public ProcessConfigPO getProcessConfigById(int processConfigId) {
        return configRepository.getProcessConfigById(processConfigId);
    }

    @Override
    public ProcessConfigPO getProcessConfigByKey(String processConfigKey) {
        return configRepository.getProcessConfigByKey(processConfigKey);
    }

    @Override
    public BaseOperationRes editProcessConfig(EditProcessConfigReq req) {
        //检查流程配置是否已存在
        ProcessConfigPO existedConfig = configRepository.getProcessConfigById(req.getProcessConfigId());
        if (existedConfig == null)
            return OperationResUtils.fail("流程配置不存在");

        ProcessConfigPO configPO = new ProcessConfigPO();
        configPO.setProcessConfigId(req.getProcessConfigId());
        configPO.setProcessConfigKey(req.getProcessConfigKey());
        configPO.setProcessCategoryId(req.getProcessCategoryId());
        configPO.setProcessDefinitionId(req.getProcessDefinitionId());
        configPO.setDescription(req.getDescription());
        configPO.setUpdateTime(new Date());

        boolean success = configRepository.editProcessConfig(configPO);
        if (!success)
            return OperationResUtils.fail("编辑流程配置失败");

        return OperationResUtils.success();
    }
}
